from collections import defaultdict


class Solution:
    def countLargestGroup(self, n: int) -> int:

        cnt = defaultdict(int)
        for i in range(1, n + 1):
            sm = 0
            while i > 0:
                sm += i % 10
                i //= 10
            cnt[sm] += 1
        ans = mx = 0
        for k, v in cnt.items():
            if v > mx:
                mx = v
                ans = 1
            elif v == mx:
                ans += 1
        return ans
